package itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.sql.DriverManager;

/**
 * @author wz
 * @description:
 * @date 2021-10-31
 */
@Configuration
@ComponentScan(basePackages = {"itheima"})
@PropertySource(value = {"classpath:jdbc.properties"},encoding = "UTF-8")
public class SpringConfig {

    @Value("${jdbc.userName}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driverClass}")
    private String driverClass;

    @Bean("dataSource")
    public DataSource dataSource(){
        DriverManagerDataSource dd = new DriverManagerDataSource();
        dd.setUsername(userName);
        dd.setPassword(password);
        dd.setUrl(url);
        dd.setDriverClassName(driverClass);
        return dd;
    }


    @Bean("druidDataSource")
    public DataSource druidDataSource(){
        DruidDataSource dd = new DruidDataSource();
        dd.setUsername(userName);
        dd.setPassword(password);
        dd.setDriverClassName(driverClass);
        dd.setUrl(url);
        return dd;
    }


    //    @Bean("jdbcTemplate")
//    public JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource){
//        JdbcTemplate jdbcTemplate = new JdbcTemplate();
//        jdbcTemplate.setDataSource(dataSource);
//        return jdbcTemplate;
//    }
    @Bean("jdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource ds){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(ds);
        return jdbcTemplate;
    }
}
